import 'package:flutter/material.dart';
import 'package:flutter_news/common/provider/provider.dart';
import 'package:flutter_news/common/router/router.dart';
import 'package:flutter_news/global.dart';
import 'package:flutter_news/pages/index/index.dart';
import 'package:flutter_news/routes.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

void main() async {
  await ScreenUtil.ensureScreenSize();
  await Global.init();
  //  Global.init().then((e) => runApp(MyApp()));
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider<AppState>.value(
          value: Global.appState,
        ),
      ],
      child: Consumer<AppState>(
        builder: (context, appState, _) {
          if (appState.isGrayFilter) {
            // 将整个 app 变为灰度模式
            return ColorFiltered(
              colorFilter: ColorFilter.mode(Colors.white, BlendMode.color),
              child: MyApp(),
            );
          }
          return MyApp();
        },
      ),
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final _appRouter = AppRouter();
    return MaterialApp.router(
      routerConfig: _appRouter.config(),
      title: 'Material App',

      // builder: (context, widget) {
      //   return MediaQuery(
      //     ///Setting font does not change with system font size
      //     // 这里保证字体不跟随系统缩放
      //     data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
      //     child: widget!,
      //   );
      // },

      // 去掉debug那个标识
      debugShowCheckedModeBanner: false,
    );

    // return MaterialApp(
    //   title: 'Material App',
    //   // builder: (context, widget) {
    //   //   return MediaQuery(
    //   //     ///Setting font does not change with system font size
    //   //     // 这里保证字体不跟随系统缩放
    //   //     data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
    //   //     child: widget!,
    //   //   );
    //   // },
    //   routes: staticRoutes,
    //   home: IndexPage(),
    //   // 去掉debug那个标识
    //   debugShowCheckedModeBanner: false,
    // );
  }
}
